﻿/**************************************************************************
创建时间:	2019/9/10
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using ASmile.Com;
using ASmile.DBModel;
using ASmile.Extend;
using ASmile.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ASmile.Win
{
    public static class ComBoxExtend
    {
        /// <summary>
        /// 设置字典的下拉列表
        /// </summary>
        /// <param name="coBox">ComboBox控件</param>
        /// <param name="dictType">字典类型</param>
        /// <param name="defaultValue">缺省选中项的 valueMember </param>
        public static ComboBox SetDataSource(this ComboBox coBox, IList dataSource, string displayMember, string valueMember, string defaultValue = null)
        {
            coBox.DataSource = dataSource;
            coBox.DisplayMember = displayMember;
            coBox.ValueMember = valueMember;
            if (defaultValue.IsNotEmpty())
            {
                coBox.SelectedValue = defaultValue;
            }
            else
            {
                if (coBox.Items.Count > 0) coBox.SelectedIndex = 0;
            }
            return coBox;
        }

        public static ComboBox SetDataSource(this ToolStripComboBox tsBox, IList dataSource, string displayMember, string valueMember, string defaultText = null)
        {
            return tsBox.ComboBox.SetDataSource(dataSource, displayMember, valueMember, defaultText);
        }

        /// <summary>
        /// 绑定公共字段数据源
        /// </summary>
        public static void SetDict(this ComboBox coBox, List<Dict_Common> dictList, string defaultText = null)
        {
            coBox.SetDataSource(dictList, "DictValue", "DictCode", defaultText);
            coBox.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 绑定公共字段数据源
        /// </summary>
        public static void SetDict(this ComboBox coBox, List<Dict_Common> dictList, short type, string defaultText = null)
        {
            var data = dictList.FindAll(s => s.DictType == type);
            coBox.SetDataSource(data, "DictValue", "DictCode", defaultText);
            coBox.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 绑定公共字段数据源
        /// </summary>
        public static void SetDict(this ComboBox coBox, List<Dict_Common> dictList, EmDictType type, string defaultText = null)
        {
            SetDict(coBox, dictList, ((short)type), defaultText);
        }


        /// <summary>
        /// 绑定公共字段数据源
        /// </summary>
        public static void SetDict(this ComboBox coBox, short type, string defaultText = null)
        {
            var data = DictHelper.GetDicts(type);
            coBox.SetDict(data, type, defaultText);
        }

        /// <summary>
        /// 绑定公共字段数据源
        /// </summary>
        public static void SetDict(this ComboBox coBox, EmDictType type, string defaultText = null)
        {
            SetDict(coBox, ((short)type), defaultText);
        }

        #region 绑定枚举

        class EnumModel
        {
            public int Value { set; get; }

            public string Text { set; get; }

            public override string ToString()
            {
                return Text;
            }
        }
        /// <summary>
        /// 绑定枚举，isQuery = true 增加“全部”查询使用
        /// </summary>
        public static void BindEnum<TEnum>(this ComboBox cBox, bool isQuery = false) where TEnum : Enum
        {
            BindEnum<TEnum>(cBox, null, isQuery);
        }
        /// <summary>
        /// 绑定枚举，当过滤返回true，则跳过绑定
        /// </summary>
        public static void BindEnum<TEnum>(this ComboBox cBox, Func<TEnum, bool> filterHanle, bool isQuery = false) where TEnum : Enum
        {
            var enValues = Enum.GetValues(typeof(TEnum));
            List<EnumModel> datas = new List<EnumModel>();
            if (isQuery)
            {
                datas.Add(new EnumModel { Value = -9999, Text = "全部" });
            }
            foreach (TEnum item in enValues)
            {
                if (filterHanle?.Invoke(item) == true)
                {
                    continue;
                }

                datas.Add(new EnumModel
                {
                    //Value = Convert.ToInt32(item),
                    //Text = item.ToString()
                    Value = Convert.ToInt32(item),
                    Text = item.ToString()
                });
            }
            cBox.ValueMember = "Value";
            cBox.DisplayMember = "Text";
            cBox.DataSource = datas;
            cBox.SelectedIndex = 0;
        }
        /// <summary>
        /// 将下拉列表的选中项值转换为绑定的枚举（需要操作过 BindEnum() 方法的数据源才可以操作）
        /// </summary>
        public static TEnum ToEnum<TEnum>(this ComboBox coBox) where TEnum : Enum
        {
            var data = coBox.SelectedItem as EnumModel;
            EnumHelper.StringToEnum(data.Text, out TEnum value);
            return value;
        }

        #endregion
    }
}